const perfix = "http://49.232.171.53:3000/api";

const http = {
  get(url, params) {
    if (params) {
      url += "?";
      let arr = [];
      for (let key in params) {
        arr.push(key + "=" + params[key]);
      }
      url += arr.join("&");
    }
    return new Promise((resolve) => {
      return fetch(perfix + url)
        .then((res) => res.json())
        .then((res) => {
          // console.log("a", res);
          if (res.status === 0) {
            resolve(res);
          } else {
            alert(res.msg);
          }
        });
    });
  },
  post(url, params) {
    return new Promise((resolve) => {
      return fetch(perfix + url, {
        method: "POST", // or 'PUT'
        body: JSON.stringify(params), // data can be `string` or {object}!
        headers: new Headers({
          "Content-Type": "application/json",
        }),
      })
        .then((res) => res.json())
        .then((res) => {
          // if (res.status === 0) {
          resolve(res);
        });
    });
  },
};

export default http;
